﻿using DataAccess;
using DataAccess.DAL;
using DataAccess.DTO;
using DataAccess.Models;
using DataAccess.ViewModel.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Api.Controllers
{


    /// <summary>
    /// 机器人相关接口
    /// </summary>
    public class RobotController : ApiController
    {
        /// <summary>
        /// 机器人登录`  
        /// </summary>
        /// <param name="account">机器人账号</param>
        /// <param name="password">机器人密码</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/robot/login")]
        public HttpResponseMessage Login(string account, string password)
        {
            if (string.IsNullOrEmpty(account) || account.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人账号格式不正确" });
            }
            if (string.IsNullOrEmpty(password) || password.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人密码格式不正确" });
            }

            RobotDAL robotDAL = new RobotDAL();
            Robot robot = new Robot();
            robot = robotDAL.Get(account, password);

            if (robot == null)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人不存在" });
            }

            robot.last_date = DateTime.Now;
            if (robotDAL.Update(robot))
            {

                RobotLoginVModel vm = new RobotLoginVModel();

                vm.activation = robot.activation;
                vm.name = robot.name;
                vm.activation = robot.activation;
                vm.name = robot.name;
                vm.state = robot.state;
                vm.account = robot.account;
                vm.promoter_id = robot.promoter_id;
                vm.last_date = robot.last_date;
                vm.Ticket = EncodeSecurity.EncryptString(string.Format("{0}&{1}&{2}", robot.account, robot.password, XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now)));
                return ResultHand.OnResult(new Result
                {
                    State = true,
                    Message = "登录成功",
                    Datas = vm
                });
            }
            return ResultHand.OnResult(new Result { State = false, Message = "登录失败" });
        }


       /// <summary>
        /// 根据群名称获取代理信息
       /// </summary>
        /// <param name="model"> 机器人id、 群名称 格式：["群名称1","群名称2","群名称3",....]、群类型   </param>
       /// <returns></returns>
        [HttpPost]
        [Route("api/robot/getgroupinfo")]
        [RobotApiAuthorize]
        public HttpResponseMessage GetGroupInfo(GetGroupInfoPostModel model)
        {
            string content = model.flock_names;// req.Content.ReadAsStringAsync().Result;
            List<string> groups = XYD.Common.JsonHelper.DeserializeJson<List<string>>(content);

            List<Agency_Flock_Info> list = new AgencyRobotDAL().GetFlockInfos(model.robot_id, model.flock_type, groups);

            return ResultHand.OnResult(new Result
            {
                State = true,
                Message = "获取信息成功",
                Datas = list
            });

            //List<AgencyUsers> users = new UsersDAL().GetUsersByGroupName(groups);

            //return ResultHand.OnResult(new Result
            //{
            //    State = true,
            //    Message = "获取信息成功",
            //    Datas =
            //        from c in users
            //        select new
            //        {
            //            state = c.state,
            //            send_interval = c.send_interval,
            //            send_removal = c.send_removal,
            //            send_stage = c.send_stage,
            //            group_name = c.group_name,
            //            group_robot_name = c.group_robot_name,
            //            group_wx_nickname = c.group_wx_nickname,
            //            label = c.label,
            //            alimama_id = c.alimama_id
            //        }
            //});



        }

        /// <summary>
        /// 注册机器人账号
        /// </summary>
        /// <param name="activation">激活码</param>
        /// <param name="account">机器人账号</param>
        /// <param name="password">机器人密码</param>
        /// <param name="repassword">机器人重复密码</param>
        /// <returns></returns>
        [Route("api/robot/regition")]
        public HttpResponseMessage Regition(string activation, string account, string password, string repassword)
        {
            Result result = new Result();

            if (string.IsNullOrEmpty(account) || account.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人账号格式不正确" });
            }
            if (string.IsNullOrEmpty(password) || password.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人密码格式不正确" });
            }
            if (password.Contains("&"))
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人密码不能包含特殊字符&" });
            }

            if (string.IsNullOrEmpty(repassword) || repassword.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人重复密码格式不正确" });
            }
            if (repassword != password)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人密码和重复密码不一致" });
            }

            RobotDAL robotDAL = new RobotDAL();
            Robot robot = new Robot();
            robot = robotDAL.Get(activation);

            if (robot == null)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人激活码不存在" });
            }

            robot.account = account;
            robot.password = password;
            robot.last_date = DateTime.Now;
            robotDAL.Update(robot);


            return ResultHand.OnResult(new Result { State = true, Message = "机器人账号注册成功" });
        }


        /// <summary>
        /// 机器人状态同步
        /// </summary>
        /// <param name="account">机器人账号</param>
        /// <param name="password">机器人密码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/robot/synchro")]
        public HttpResponseMessage Synchro(string account, string password)
        {
            if (string.IsNullOrEmpty(account) || account.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人账号格式不正确" });
            }
            if (string.IsNullOrEmpty(password) || password.Length > 50)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人密码格式不正确" });
            }

            RobotDAL robotDAL = new RobotDAL();
            Robot robot = new Robot();
            robot = robotDAL.Get(account, password);

            if (robot == null)
            {
                return ResultHand.OnResult(new Result { State = false, Message = "机器人不存在" });
            }

            robot.last_date = DateTime.Now;
            if (robotDAL.Update(robot))
            {
                return ResultHand.OnResult(new Result
                {
                    State = true,
                    Message = "状态同步成功"
                });
            }

            return ResultHand.OnResult(new Result { State = false, Message = "状态同步失败" });
        }



        /// <summary>
        /// 设置代理微信发单状态
        /// </summary>
        /// <param name="agent_id"></param>
        /// <returns></returns>
        public HttpResponseMessage SetAgentWxSendType(string promoter_id,string agent_id,string state)
        {

            var key = RedisKey.AgentIndependentSendType(promoter_id,agent_id);

            //-1没有登录或掉线0等待扫码1正在扫码2扫码失败3加载数据4正在发单
            //var type = RedisHelper.Single_Get_Itme<string>(RedisKey.SysConfig(key));

            if (!string.IsNullOrEmpty(state))
            {
                RedisHelper.Single_Remove_Itme(key);
                RedisHelper.Single_Set_Itme<string>(key, state);
            }
            return ResultHand.OnResult(new Result { State = true, Message = "更新成功" });
        }


    }
}